package util;

import javax.swing.JPanel;
import javax.swing.JTextField;

public class Util {

	public static double roundToDecimals(double inputNumber, int decimals) {
		return Math.round((inputNumber * Math.pow(10, decimals)))
				/ Math.pow(10, decimals);
	}

	public static NumericCheckWrapper checkNumeric(String stringInput) {
		if (stringInput.length() == 0) {
			return new NumericCheckWrapper(0.0, true);
		} else {
			try {
				return new NumericCheckWrapper(roundToDecimals(Double.valueOf(stringInput), 2), true);
			} catch (NumberFormatException e1) {
				return tryCheckWithComma(stringInput);
			}
		}
	}

	private static NumericCheckWrapper tryCheckWithComma(String stringInput) {
		if (stringInput.indexOf(",") != -1) {
			stringInput = stringInput.replace(",", ".");
			try {
				return new NumericCheckWrapper(roundToDecimals(Double.valueOf(stringInput), 2), true);
			} catch (NumberFormatException e2) {
				return new NumericCheckWrapper(0.0, false);
			}
		} else {
			return new NumericCheckWrapper(0.0, false);
		}
	}
	
	public static JTextField createOutputTextField(JPanel panel, Double value){
		JTextField result = new JTextField(value == 0 ? "N/A" : "" + value);
		result.setEditable(false);
		panel.add(result);
		return result;
	}
}
